package com.atguigu.exam.config;

import com.atguigu.exam.properties.KimiProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.client.WebClient;

@EnableConfigurationProperties(value = KimiProperties.class)
@SpringBootConfiguration
public class KimiConfiguration {

    @Autowired
    private KimiProperties kimiProperties;

    @Bean
    public WebClient webClient(){
        //创建WebClient对象
        WebClient webClient = WebClient
                .builder()
                //设置一个基础路径
                .baseUrl(kimiProperties.getBaseUrl())
                //设置请求数据的类型是JSON格式
                .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
                //设置apiKey
                .defaultHeader(HttpHeaders.AUTHORIZATION, "Bearer " + kimiProperties.getApiKey())
                .build();
        return webClient;
    }
}
